package com.lilicould.controller;

import com.lilicould.entity.Tenant;
import com.lilicould.service.impl.TenantServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/update/tenant")
public class UpdateTenantController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取修改后的参数
        String name = req.getParameter("name");
        String bankAccount = req.getParameter("bankAccount");
        String emergencyName = req.getParameter("emergencyName");
        String emergencyPhone = req.getParameter("emergencyPhone");

        // 获取租客id
        Tenant tenant = (Tenant) req.getSession().getAttribute("tenant");
        String tenantId = tenant.getTenantId();

        // 获取Service
        TenantServiceImpl tenantService = new TenantServiceImpl();
        if (tenantService.updateTenant(tenantId, name, bankAccount, emergencyName, emergencyPhone)) {
            req.getSession().invalidate(); // 清除session
            req.getSession().setAttribute("success", "已成功修改您的信息");
            req.getRequestDispatcher("/page/success.jsp").forward(req, resp);
        } else {
            req.getSession().setAttribute("error", "修改失败");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("error","请求应为POST方式");
        req.getRequestDispatcher("/page/err.jsp").forward(req,resp);
    }
}
